﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Input;
using Autorent.Desktop.Main;
using Autorent.Desktop.Model;
using Autorent.Desktop.Model.Services;
using Autorent.Desktop.Model.AutorentMngrServiceReference;
using Autorent.Desktop.Modules.Events;
using Autorent.Desktop.Modules.Clients.Services;
using Autorent.Desktop.Modules.Clients.Views;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Prism.Events;
using Microsoft.Practices.Prism.Regions;

namespace Autorent.Desktop.Modules.Clients.ViewModels
{
    public class ClientsListViewModel : ViewModelBase
    {
        #region Public Properties

        public ClientsListView View { get; set; }

        public ObservableCollection<Client> Clients
        {
            get
            {
                return _repository.GetClients();
            }
        }

        public Client SelectedClient { get; set; }

        #endregion

        #region Constructors

        public ClientsListViewModel(ClientsListView view, IClientsRepository repository, IUnityContainer container, 
            IRegionManager regionManager, IEventAggregator eventAggregator)
        {
            View = view;
            View.Model = this;

            _repository = repository;
            _regionManager = regionManager;
            _container = container;
            _eventAggregator = eventAggregator;
            _eventAggregator.GetEvent<CollectionChangedEvent>().Subscribe(new Action<string>(OnPropertyChanged));
        }

        #endregion

        #region Commands

        public ICommand CreateClient
        {
            get
            {
                return new RelayCommand(param => createClient());
            }
        }

        public ICommand UpdateClient
        {
            get
            {
                return new RelayCommand(param => updateClient());
            }
        }

        public ICommand DeleteClient
        {
            get
            {
                return new RelayCommand(param => deleteClient());
            }
        }

        public ICommand SearchClient
        {
            get
            {
                return new RelayCommand(param => searchClient());
            }
        }

        #endregion

        #region Private Variables

        private readonly IClientsRepository _repository;
        private readonly IRegionManager _regionManager;
        private readonly IUnityContainer _container;
        private readonly IEventAggregator _eventAggregator;

        #endregion

        #region Private Methods

        private void createClient()
        {
            object view = _regionManager.Regions["DetailsRegion"].GetView("CreateClientView");

            if (view == null) 
            {
                foreach (var views in _regionManager.Regions["DetailsRegion"].ActiveViews)
                {
                    _regionManager.Regions["DetailsRegion"].Remove(views);
                }

                _regionManager.Regions["DetailsRegion"].Add(_container.Resolve<CreateClientViewModel>().View, "CreateClientView"); 
            }
            else { _regionManager.Regions["DetailsRegion"].Remove(view); }
        }

        private void updateClient()
        {
            if (SelectedClient != null)
            {
                object view = _regionManager.Regions["DetailsRegion"].GetView("UpdateClientView");

                if (view == null) 
                {
                    foreach (var views in _regionManager.Regions["DetailsRegion"].ActiveViews)
                    {
                        _regionManager.Regions["DetailsRegion"].Remove(views);
                    }

                    _regionManager.Regions["DetailsRegion"].Add(_container.Resolve<UpdateClientViewModel>(new ParameterOverrides() { { "client", SelectedClient }}).View, "UpdateClientView"); 
                }
                else { _regionManager.Regions["DetailsRegion"].Remove(view); }
            }
            else
            {
                MessageBox.Show("Select client!", "Information", MessageBoxButton.OK, MessageBoxImage.Information);
            }
        }

        private void deleteClient()
        {
            _repository.DeleteClient(SelectedClient);
            OnPropertyChanged("Clients");
        }

        private void searchClient()
        {
            object view = _regionManager.Regions["SearchRegion"].GetView("SearchClientView");

            if (view == null)
            {
                _regionManager.Regions["SearchRegion"].Add(_container.Resolve<SearchClientViewModel>().View, "SearchClientView");
            }
            else { _regionManager.Regions["SearchRegion"].Remove(view); }
        }

        #endregion
    }
}
